﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Data.Sql;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;
using YouHP.Model;

namespace YouHP.Atom
{
    public class PatientXiaoChuan
    {
        static readonly string _sGetHistoriesByPrId = "GetXiaoChuanHistoryByPrId";
        static readonly string _sAddHistory = "AddXiaoChuanHistory";
        static readonly string _sUpdateHistory = "UpdateXiaoChuanHistory";
        static readonly string _sDeleteHistory = "DeleteXiaoChuanHistory";
        static readonly string _sGetHistory = "GetXiaoChuanHistoryById";
       
        public List<XiaoChuanInfo> GetHistoriesByPrId(int prId, DateTime dStart, DateTime dEnd, int iPageSize, int iPageNo, out int iCount)
        {
            DataSet ds = SqlHelper.ExecuteDataset(Common.ConnStr,
               _sGetHistoriesByPrId, prId, dStart, dEnd, iPageSize, iPageNo);
            IDataReader dr = ds.Tables[0].CreateDataReader();
            if (dr != null && dr.Read())
            {
                iCount = dr.GetInt32(0);
                dr.Close();
            }
            else
            {
                iCount = 0;
            }
            dr = ds.Tables[1].CreateDataReader();
            return CBO.FillCollection<XiaoChuanInfo>(dr);
        }



        public int AddRecord(XiaoChuanInfo info)
        {
            SqlParameter[] outParms = new SqlParameter[]  {
                new SqlParameter("@PRId", SqlDbType.Int),
                new SqlParameter("@Indicator", SqlDbType.NVarChar, 256),
                new SqlParameter("@Data", SqlDbType.Int),
                new SqlParameter("@RecordDate", SqlDbType.DateTime),
                new SqlParameter("@Id", SqlDbType.Int)
            };
            outParms[0].Value = info.PrId;
            outParms[1].Value = info.Indicator;
            outParms[2].Value = info.Data;
            outParms[3].Value = info.RecordDate;
            outParms[4].Direction = ParameterDirection.Output;

            SqlHelper.ExecuteNonQuery(Common.ConnStr, CommandType.StoredProcedure, _sAddHistory, outParms);
            info.Id = Convert.ToInt32(outParms[4].Value.ToString());
            return 0; 
        }

        public int UpdateRecord(XiaoChuanInfo info)
        {
            return SqlHelper.ExecuteNonQuery(Common.ConnStr,
                _sUpdateHistory,
                info.Id,
                info.PrId,
                info.Indicator, 
                info.Data,
                info.RecordDate
                );
        }

        public int DeleteRecord(XiaoChuanInfo info)
        {
            return SqlHelper.ExecuteNonQuery(Common.ConnStr, _sDeleteHistory, info.Id, info.PrId);
        }

        public XiaoChuanInfo GetRecord(int iId, int iPrId)
        {
            return CBO.FillObject<XiaoChuanInfo>(SqlHelper.ExecuteReader(Common.ConnStr, _sGetHistory, iId, iPrId));
            
        }
    }
}
